﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.VisualBasic;

namespace Smrf.NodeXL.GraphDataProviders.MediaWiki
{
    public partial class MediaWikiLoginDialog : Form
    {
        private String cookieContent;        
        private MediaWikiGraphDataProviderDialogBase fcbDialog;
        private string sHost;

        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]        
        static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref uint pcchCookieData, int dwFlags, IntPtr lpReserved);
        const int INTERNET_COOKIE_HTTPONLY = 0x00002000;

        public static string GetHTTPCookies(string uri)
        {
            uint datasize = 1024;
            StringBuilder cookieData = new StringBuilder((int)datasize);
            if (InternetGetCookieEx(uri, "JSESSIONID", cookieData, ref datasize, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero)
                && cookieData.Length > 0)
            {
                return cookieData.ToString();
            }
            else
            {
                return null;
            }
        }

        public MediaWikiLoginDialog(MediaWikiGraphDataProviderDialogBase fcbDialog, string sHost)
        {
            InitializeComponent();            
            this.fcbDialog = fcbDialog;
            this.sHost = sHost;
        }        

        public void LogIn()
        {            
            string url = sHost + "/login.jspa";            
            //Fiddler.FiddlerApplication.ResponseHeadersAvailable += new Fiddler.SessionStateHandler(FiddlerApplication_ResponseHeadersAvailable);
            webBrowser1.Navigate(url);
            this.Text = sHost + " - Login";
            this.ShowDialog();
        }

        public void LogOut()
        {            
            webBrowser1.Navigate(sHost+"/logout.jspa");
            this.ShowDialog();
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {           
              
        }

        private void deleteCookies()
        {
            DirectoryInfo folder = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
            //Get the list file in temporary
            FileInfo[] files = folder.GetFiles();           
            foreach (FileInfo file in files)
            {                
                try
                {
                    System.IO.File.Delete(file.FullName);
                }
                catch (System.Exception e)
                {
                    MessageBox.Show(e.Message);
                }

            }            
            
        }

        private void DisableComponents(MediaWikiGraphDataProviderDialogBase fcbDialog)
        {
            if (fcbDialog is MediaWikiPageDialog)
            {
                MediaWikiPageDialog CastedFcbDialog = (MediaWikiPageDialog)fcbDialog;                
                CastedFcbDialog.dgAttributes.Enabled = true;                
                CastedFcbDialog.chkSelectAll.Enabled = true;
                //CastedFcbDialog.txtForumID.Enabled = true;
                //CastedFcbDialog.txtThreadID.Enabled = true;
                //CastedFcbDialog.btnSelectForum.Enabled = true;                
                //CastedFcbDialog.txtHost.Enabled = false;
            }
            //else if (fcbDialog is FacebookFanPageDialog)
            //{
            //    FacebookFanPageDialog CastedFcbDialog = (FacebookFanPageDialog)fcbDialog;
            //    CastedFcbDialog.btnOK.Enabled = true;
            //    //CastedFcbDialog.txtPageUsernameID.Enabled = false;
            //    CastedFcbDialog.dgAttributes.Enabled = false;
            //    CastedFcbDialog.chkSelectAll.Enabled = false;
            //    //CastedFcbDialog.chkCoCommenters.Enabled = false;
            //    //CastedFcbDialog.chkCoLikers.Enabled = false;
            //    //CastedFcbDialog.nudFirstPosts.Enabled = false;
            //    CastedFcbDialog.chkStatusUpdates.Enabled = false;
            //    CastedFcbDialog.chkWallPosts.Enabled = false;
            //    //CastedFcbDialog.rbDownloadFirstPosts.Enabled = false;
            //    //CastedFcbDialog.chkUserPost.Enabled = false;
            //    //CastedFcbDialog.chkPostPost.Enabled = false;
            //    //CastedFcbDialog.rbDateDownload.Enabled = false;
            //    //CastedFcbDialog.dtPosts.Enabled = false;
            //}
        }

        public String CookieContent
        {
            get { return this.cookieContent; }
        }

        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
        {
            //e.Cancel = true;
            //webBrowser1.Navigate(((WebBrowser)sender).Url);
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            String stringUrl = webBrowser1.Url.ToString();            
            //if (stringUrl.StartsWith(lwDialog.Host + "/index.jspa"))
            //{
            //    cookieContent = webBrowser1.Document.Cookie;
            //    DisableComponents(fcbDialog);
            //    this.Visible = false;
            //} 

            
            string cookies = webBrowser1.Document.Cookie+";"+
                GetHTTPCookies(webBrowser1.Document.Url.AbsoluteUri);

            
            if (IsAuthenticated(sHost, cookies))
            {                
                cookieContent = cookies;
                DisableComponents(fcbDialog);
                this.Visible = false;                
            }
            
            
        }

        
        private bool IsAuthenticated(string host, string cookie)
        {
            return true;
        }
        
    }
}
